package com.topboya.linker.ui.presenter;


import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.CreateDocCustomerResponse;
import com.topboya.linker.model.response.CustomerByOrgIdAndNewlyCusListResopnse;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.DictByKeyResponse;
import com.topboya.linker.model.response.NewlyOrgListResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.CustomerView;
import com.topboya.linker.ui.view.MyCustomerView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyCustomerPresenter extends BasePresenter<MyCustomerView> {

    private static final String TAG = "CustomerPresenter-";

    public MyCustomerPresenter(BaseActivity context) {
        super(context);

    }

    public void getOrgList(String pageNo,String orgName,String delType) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getOrgListMy(pageNo,orgName,delType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(organizationResopnse -> {

                    String success = organizationResopnse.getMeta().getSuccess();
                    if (null != success && Boolean.parseBoolean(success)) {
                        ArrayList<OrganizationResopnse.DocDetailInfoList>  docDetailInfoListOrg = (ArrayList<OrganizationResopnse.DocDetailInfoList>) organizationResopnse.getData().getDocDetailInfoList();
                         getView().setCustomeData(docDetailInfoListOrg);
                        mContext.hideWaitingDialog();
                    }
                }, this::getOrgError);
    }

    public void getOrgListInAudited(String pageNo,String orgName,String delType) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getOrgListMy(pageNo,orgName,delType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(organizationResopnse -> {

                    String success = organizationResopnse.getMeta().getSuccess();
                    if (null != success && Boolean.parseBoolean(success)) {
                        ArrayList<OrganizationResopnse.DocDetailInfoList>  docDetailInfoListOrg = (ArrayList<OrganizationResopnse.DocDetailInfoList>) organizationResopnse.getData().getDocDetailInfoList();
                        getView().getOrgListInAudited(docDetailInfoListOrg);
                        mContext.hideWaitingDialog();
                    }
                }, this::getOrgError);
    }
    private void getOrgError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "getOrgError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();

    }


}
